$(function () {
    layui.use(['form', 'layer', 'jquery', 'laydate'],

        function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;
            var laydate = layui.laydate;

            findById();

            function findById() {
                let id = sessionStorage.getItem("userId");
                $.ajax({
                    url: '/back/user/findById',
                    data: {id: id},
                    type: 'get',
                    dataType: "json",
                    success: function (res) {
                        setData(res.data);
                    }
                })
            }

            function setData(data) {
                // console.log(data)
                form.val("example", {
                    "username": data.username,
                    "birthday": data.birthday,
                    "phone": data.phone,
                    "email": data.email,
                    "address": data.address,
                    "sex": data.sex,
                });
                $("input[type=radio][name=enable][value=" + data.enable + "]").attr("checked", "checked")
                // 重新渲染表单
                form.render();
            }

            //日期
            laydate.render({
                elem: '#birthday',
                trigger: "click"
            });

            //自定义验证规则
            form.verify({
                username: [/^[a-zA-Z0-9]{5,16}/, '用户名必须6到16为字符'],
            });

            //监听提交
            form.on('submit(update)',
                function (data) {
                    data = data.field;
                    console.log(data);
                    data.id = sessionStorage.getItem("userId")
                    $.ajax({
                        url: '/back/user/update',
                        data: data,
                        type: 'post',
                        dataType: 'json',
                        success: function (res) {
                            if (res.count > 0) {
                                layer.alert("修改成功", {
                                        icon: 6
                                    },
                                    function () {
                                        //关闭当前frame
                                        xadmin.close();

                                        // 可以对父窗口进行刷新
                                        xadmin.father_reload();
                                    });
                            } else {
                                layer.alert("修改失败",
                                    function () {
                                        //关闭当前frame
                                        xadmin.close();

                                        // 可以对父窗口进行刷新
                                        xadmin.father_reload();
                                    });
                            }
                        }
                    })
                    return false;
                });
        });
})